const path = require("path");
const { crateGoods, findGoodsAll,updateGoods,updateOrder,updateOneGoods,updateRemark ,deleteGoods} = require("../service/goods.service");
const { APP_PORT,APP_HOST } = require("../config/config.default.js");
class Goods {
  upload(ctx, next) {
    // console.log(ctx.request.files["file\n"]._writeStream.path, "files");
    const { _writeStream } = ctx.request.files["file"];

    ctx.body = {
      code: 0,
      msg: "上传成功",
      resule: {
        goods_img: `http://${APP_HOST}:${process.env.APP_PORT}/${path.basename(_writeStream.path)}`,
      },
    };
  }
  async updateOneBill(ctx){
      await updateOneGoods(ctx)
      ctx.body={code:0}
  }
  async delGoods(ctx){
      await deleteGoods(ctx.request.params.id)
      ctx.body = {
          code:0,
          msg:'删除成功'
      }
  }
  async create(ctx, next) {
    try {
      await crateGoods(ctx.request.body);
      ctx.body = {
        code: 0,
        msg: "添加成功",
      };
    } catch (e) {
      console.log(e, "e");
    }
  }
  async doneOrder(ctx,){
    
      await updateOrder(ctx.request.body.table)
      ctx.body= {
          code:0,
          msg:"结账成功"
      }
  }
  async update(ctx, next) {

         let obj = {}
         let table= ''
        let id = ''
   
    let remark = ctx.request.body.remark
   
 
    await updateRemark(remark)
    for(var i = 0;i<ctx.request.body.bill.length;i++){
          
       for(var j = 0;j<15;j++){
             table  =`table_${j+1}`
             obj[table] = ctx.request.body.bill[i][table]
        }
            id = ctx.request.body.bill[i].id
     
          let res = await updateGoods( obj,id);
    }

   
      ctx.body = {
        code: 0,
        msg: '提交成功'
      };
   
  }
  async remove(ctx, next) {
    const res = await removeGoods(ctx.params.id);
    if (res) {
      ctx.body = {
        code: 0,
        msg: "删除成功",
      };
    }
  }
  async destore(ctx, next) {
    await destoreGoods(ctx.params.id);
    ctx.body = {
      code: 0,
      msg: "商品下架成功",
    };
  }
  async restore(ctx, next) {
    await restoreGoods(ctx.params.id);
    ctx.body = {
      code: 0,
      msg: "商品上架成功",
    };
  }
  async findAll(ctx, next) {
    const res = await findGoodsAll();
    ctx.body = {
      code: 0,

      total: res.count,
      list: res.list,
    };
  }
}

module.exports = new Goods();
